import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class TcpEchoClient_0603 {
    Socket socket = null;

    public TcpEchoClient_0603(String serverIp, int serverPort) throws IOException {
        socket = new Socket(serverIp,serverPort);
    }

    public void start() {
        System.out.println("客户端启动");
        try (InputStream inputStream = socket.getInputStream();
             OutputStream outputStream = socket.getOutputStream()){
            Scanner scannerConsole = new Scanner(System.in);
            Scanner scannerNetwork = new Scanner(inputStream);
            PrintWriter printWriter = new PrintWriter(outputStream);
            while (true) {
                System.out.print("->");
                if (!scannerConsole.hasNext()) {
                    break;
                }
                String request = scannerConsole.next();
                //println  末尾换行；
                printWriter.println(request);

                printWriter.flush();

                String response = scannerNetwork.next();

                System.out.println(response);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    public static void main(String[] args) throws IOException {
        TcpEchoClient_0603 tcpEchoClient_0603 = new TcpEchoClient_0603("127.0.0.1",9090);
        tcpEchoClient_0603.start();
    }
}
